﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class SysGroupAuthority
    {
        public int Add(Chain.Model.SysGroupAuthority model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysGroupAuthority(");
            builder.Append("GroupID,ModuleID,ActionID,ActionValue)");
            builder.Append(" values (");
            builder.Append("@GroupID,@ModuleID,@ActionID,@ActionValue)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GroupID", SqlDbType.Int, 4), new SqlParameter("@ModuleID", SqlDbType.Int, 4), new SqlParameter("@ActionID", SqlDbType.Int, 4), new SqlParameter("@ActionValue", SqlDbType.Bit, 1) };
            cmdParms[0].Value = model.GroupID;
            cmdParms[1].Value = model.ModuleID;
            cmdParms[2].Value = model.ActionID;
            cmdParms[3].Value = model.ActionValue;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool CheckChildGroup(int GroupID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysGroupAuthority set ActionValue = 0 ");
            builder.Append("where ");
            builder.Append("GroupID in (select GroupID from SysGroup where ParentIDStr like @GroupIDStr) ");
            builder.Append("and ActionID not in (select ActionID from SysGroupAuthority where GroupID = @GroupID and ActionValue=1)");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GroupID", SqlDbType.Int, 4), new SqlParameter("@GroupIDStr", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = GroupID;
            cmdParms[1].Value = "%/" + GroupID + "/%";
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public Chain.Model.SysGroupAuthority DataRowToModel(DataRow row)
        {
            Chain.Model.SysGroupAuthority authority = new Chain.Model.SysGroupAuthority();
            if (row != null)
            {
                if ((row["GAID"] != null) && (row["GAID"].ToString() != ""))
                {
                    authority.GAID = int.Parse(row["GAID"].ToString());
                }
                if ((row["GroupID"] != null) && (row["GroupID"].ToString() != ""))
                {
                    authority.GroupID = new int?(int.Parse(row["GroupID"].ToString()));
                }
                if ((row["ModuleID"] != null) && (row["ModuleID"].ToString() != ""))
                {
                    authority.ModuleID = new int?(int.Parse(row["ModuleID"].ToString()));
                }
                if ((row["ActionID"] != null) && (row["ActionID"].ToString() != ""))
                {
                    authority.ActionID = new int?(int.Parse(row["ActionID"].ToString()));
                }
                if ((row["ActionValue"] == null) || !(row["ActionValue"].ToString() != ""))
                {
                    return authority;
                }
                if ((row["ActionValue"].ToString() == "1") || (row["ActionValue"].ToString().ToLower() == "true"))
                {
                    authority.ActionValue = true;
                    return authority;
                }
                authority.ActionValue = false;
            }
            return authority;
        }

        public bool Delete(int GAID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysGroupAuthority ");
            builder.Append(" where GAID=@GAID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GAID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GAID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(int GroupID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysGroupAuthority ");
            builder.Append(" where GroupID=" + GroupID);
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool DeleteList(string GAIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysGroupAuthority ");
            builder.Append(" where GAID in (" + GAIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int GAID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysGroupAuthority");
            builder.Append(" where GAID=@GAID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GAID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GAID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select GAID,GroupID,ModuleID,ActionID,ActionValue ");
            builder.Append(" FROM SysGroupAuthority ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataTable GetList(int groupID, int moduleID, int actionID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from SysGroupAuthority ");
            builder.Append("where GroupID=@groupID ");
            builder.Append("and ModuleID=@moduleID ");
            builder.Append("and ActionID=@actionID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@groupID", SqlDbType.Int, 4), new SqlParameter("@moduleID", SqlDbType.Int, 4), new SqlParameter("@actionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = groupID;
            cmdParms[1].Value = moduleID;
            cmdParms[2].Value = actionID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms).Tables[0];
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" GAID,GroupID,ModuleID,ActionID,ActionValue ");
            builder.Append(" FROM SysGroupAuthority ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.GAID desc");
            }
            builder.Append(")AS Row, T.*  from SysGroupAuthority T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("GAID", "SysGroupAuthority");
        }

        public Chain.Model.SysGroupAuthority GetModel(int GAID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 GAID,GroupID,ModuleID,ActionID,ActionValue from SysGroupAuthority ");
            builder.Append(" where GAID=@GAID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GAID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GAID;
            new Chain.Model.SysGroupAuthority();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public int getModuleID(string AbsolutePath)
        {
            int num = 0;
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("DECLARE @Url NVARCHAR(100);SET @Url='{0}';DECLARE @Pid INT ;SET @Pid=0;SELECT TOP 1 @Pid=a.ModuleID FROM dbo.SysModule a WITH(NOLOCK) WHERE a .ModuleParentID>0 AND ModuleLink=@Url;IF @Pid=0 BEGIN SET @Url=@Url+'?PID='; SELECT TOP 1 @Pid=a.ModuleID FROM dbo.SysModule a WITH(NOLOCK) WHERE a.ModuleLink=@Url+CAST(a.ModuleID AS NVARCHAR(10)); END SELECT @Pid AS PID", AbsolutePath);
            DataTable table = DbHelperSQL.Query(builder.ToString()).Tables[0];
            builder = null;
            if (table != null)
            {
                num = int.Parse(table.Rows[0][0].ToString());
            }
            return num;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysGroupAuthority ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.SysGroupAuthority model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysGroupAuthority set ");
            builder.Append("GroupID=@GroupID,");
            builder.Append("ModuleID=@ModuleID,");
            builder.Append("ActionID=@ActionID,");
            builder.Append("ActionValue=@ActionValue");
            builder.Append(" where GAID=@GAID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GroupID", SqlDbType.Int, 4), new SqlParameter("@ModuleID", SqlDbType.Int, 4), new SqlParameter("@ActionID", SqlDbType.Int, 4), new SqlParameter("@ActionValue", SqlDbType.Bit, 1), new SqlParameter("@GAID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.GroupID;
            cmdParms[1].Value = model.ModuleID;
            cmdParms[2].Value = model.ActionID;
            cmdParms[3].Value = model.ActionValue;
            cmdParms[4].Value = model.GAID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

